package us.wili.dev.client.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import us.wili.dev.client.model.req.*;
import us.wili.dev.client.model.rsp.*;
import us.wili.dev.client.service.*;
import us.wili.dev.common.model.ResponseDto;
import us.wili.dev.common.util.ResponseUtil;
import us.wili.dev.model.entity.Collection;
import us.wili.dev.model.entity.Shipaddress;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@Validated
@RestController
@RequestMapping("/api/user")
@Api(tags = "用户")
public class UserController {
    @Autowired
    private ClientShipAddressService clientShipAddressService;

    @Autowired
    private ClientCollectionServcie clientCollectionServcie;

    @Autowired
    private ClientUserService clientUserService;

    @ApiOperation(value = "收货地址查看", nickname = "shipaddress")
    @RequestMapping(value = "/shipaddress", method = RequestMethod.GET)
    public ResponseDto<ShipAddressRsp> shipaddress(Long id){
        List<ShipAddressRsp> rsp =clientShipAddressService.listaddress(id);
        return ResponseUtil.getSuccessRsp(rsp);
    }

    @ApiOperation(value = "地址查看", nickname = "oneAddress")
    @RequestMapping(value = "/oneAddress", method = RequestMethod.GET)
    public ResponseDto<ShipAddressRsp> oneAddress(Long id){
        ShipAddressRsp rsp =clientShipAddressService.oneAddress(id);
        return ResponseUtil.getSuccessRsp(rsp);
    }

    @ApiOperation(value = "添加收货地址", nickname = "addaddress")
    @RequestMapping(value = "/addaddress", method = RequestMethod.POST)
    public ResponseDto addaddress(@Valid @RequestBody ShipAddressReq req,HttpServletRequest request,HttpServletResponse response){
        Shipaddress shipAddress = clientShipAddressService.addaddress(req);
        return ResponseUtil.getSuccessRsp(shipAddress);
    }

    @ApiOperation(value = "修改收货地址", nickname = "upaddaddress")
    @RequestMapping(value = "/upaddaddress", method = RequestMethod.POST)
    public ResponseDto upaddaddress(@Valid @RequestBody UpAddressReq req,HttpServletRequest request,HttpServletResponse response){
        Shipaddress shipAddress = clientShipAddressService.upaddaddress(req);
        return ResponseUtil.getSuccessRsp(shipAddress);
    }

    @ApiOperation(value = "删除地址", nickname = "deleteAddress")
    @RequestMapping(value = "/deleteAddress", method = RequestMethod.GET)
    public ResponseDto deleteAddress(Long id) {
        clientShipAddressService.deleteAddress(id);
        return new ResponseDto();
    }

    @ApiOperation(value = "我的收藏", nickname = "collection")
    @RequestMapping(value = "/mycollection", method = RequestMethod.GET)
    public ResponseDto<ShopcartRsp> mycollection(Long id){
        List<ShopcartRsp> rsp =clientCollectionServcie.mycollection(id);
        return ResponseUtil.getSuccessRsp(rsp);
    }

    @ApiOperation(value = "加入收藏", nickname = "Joincollection")
    @RequestMapping(value = "/Joincollection", method = RequestMethod.POST)
    public ResponseDto Joincollection(@Valid @RequestBody JoinCollectionReq req,HttpServletRequest request,HttpServletResponse response){
        Collection collection = clientCollectionServcie.Joincollection(req);
        return ResponseUtil.getSuccessRsp(collection);
    }

    @ApiOperation(value = "删除收藏", nickname = "deletecollection")
    @RequestMapping(value = "/deletecollection", method = RequestMethod.GET)
    public ResponseDto deletecollection(Long id) {
        clientCollectionServcie.deletecollection(id);
        return new ResponseDto();
    }

    @ApiOperation(value = "个人中心", nickname = "AboutMe")
    @RequestMapping(value = "/AboutMe", method = RequestMethod.GET)
    public ResponseDto<AboutMeRsp> AboutMe(Long id){
        AboutMeRsp rsp =clientUserService.AboutMe(id);
        return ResponseUtil.getSuccessRsp(rsp);
    }

}
